//
//  XGL_LiveRoomChatInputView.swift
//  XGamesLive
//
//  Created by ios on 2024/5/30.
//

import UIKit

protocol ChatInputDelegate: NSObjectProtocol {
    func sendChatMessage(content: String)
}
class XGL_LiveRoomChatInputView: UIView {
    weak var delegate: ChatInputDelegate?
    var userHeadView: UserHeadView!
    var inputTextContentView: UIView!
    var inputTextView: PanTextView!
    
    private var textOriginalHeight: CGFloat = 36.0

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.createXglView()
        self.userHeadView.userData = CompleteInfor(data1: XGL_userDataManager.share.userName,data2: XGL_userDataManager.share.userHeader,userId: NSInteger(XGL_userDataManager.share.userId) ?? 0)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension XGL_LiveRoomChatInputView{
    public func restoreInitialization(){
        self.inputTextView.text = ""
        self.inputTextView.resignFirstResponder()
        self.handleTextHeight(textView: self.inputTextView)
    }
   
    private func handleTextHeight(textView: UITextView) {

        let size = textView.sizeThatFits(CGSize(width: textView.bounds.width, height: CGFloat(MAXFLOAT)))
        let currentHeight = CGFloat(ceilf(Float(size.height))) > self.textOriginalHeight ? CGFloat(ceilf(Float(size.height))) : self.textOriginalHeight
        if currentHeight > self.textOriginalHeight{
            self.inputTextView.layerCornerRadius = 4.0
        }else{
            self.inputTextContentView.layerCornerRadius = self.textOriginalHeight / 2.0
        }
        self.inputTextContentView.snp.updateConstraints { make in
            make.height.equalTo(currentHeight)
        }
        UIView.animate(withDuration: 0.25) {
            self.superview!.layoutIfNeeded()
        }
    }
}

extension XGL_LiveRoomChatInputView: UITextViewDelegate{
    func textViewDidChange(_ textView: UITextView) {

        self.handleTextHeight(textView: textView)
        
    }
    
    func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
        if text == "\n"{
            if let textValue = textView.text,textValue.isEmpty == false {
                self.delegate?.sendChatMessage(content: textValue)
            }
            return false
        }
        return true
    }
}


extension XGL_LiveRoomChatInputView{
    private func createXglView(){
        self.userHeadView = UserHeadView().then({ make in
            make.layerCornerRadius = 17.0
            make.layerBorderColor = .white.withAlphaComponent(0.8)
            make.layerBorderWidth = 1.0
        })
        let bottomView = UIView()
        self.inputTextContentView = UIView().then({ make in
            make.backgroundColor = UIColor.black.withAlphaComponent(0.5)
            make.layerCornerRadius = 18.0
            make.clipsToBounds = true
        })
        self.inputTextView = PanTextView().then({ make in
            make.textColor = UIColor.white
            make.placeholder = "Enter here"
            make.placeholderColor = UIColor.hexColor("#A2A2A2")
            make.font = UIFont.pingfangSC_Regular(fontSize: 16.0)
            make.delegate = self
            make.backgroundColor = .clear
        })
        self.inputTextContentView.addSubview(self.inputTextView)
        self.addSubviews([userHeadView,inputTextContentView,bottomView])
        userHeadView.snp.makeConstraints { make in
            make.left.equalTo(15.0)
            make.top.equalTo(1)
            make.width.height.equalTo(34.0)
        }
        inputTextView.snp.makeConstraints { make in
            make.center.height.equalToSuperview()
            make.left.equalTo(10.0)
        }
        inputTextContentView.snp.makeConstraints { make in
            make.left.equalTo(userHeadView.snp.right).offset(12.0)
            make.right.equalTo(-15.0)
//            make.top.equalToSuperview()
            make.bottom.equalTo(bottomView.snp.top)
            make.height.equalTo(36.0)
        }
        bottomView.snp.makeConstraints { make in
            make.width.centerX.bottom.equalToSuperview()
            make.height.equalTo(BottomHeight)
        }
        self.snp.makeConstraints { make in
            make.top.equalTo(inputTextContentView.snp.top)
        }
    }
}
